package com.example.takeaway.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.takeaway.common.R;
import com.example.takeaway.entity.Category;
import com.example.takeaway.service.CategoryService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/category")
@AllArgsConstructor
public class CategoryController {

    private final CategoryService categoryService;


    /**
     * 添加套餐
     * @param category
     * @return
     */

    @PostMapping
    public R<String> Add(@RequestBody Category category){

        log.info("添加分类：{}",category.toString());

        categoryService.save(category);

        return R.success("添加分类成功");

    }

    /**
     * //查询套餐 分页查询
     * @param page
     * @param pageSize
     * @return
     */

    @GetMapping("/page")
    public R<Page> page(int page , int pageSize){


        log.info("获取页数{},页面数据大小{}",page,pageSize);

        //构建分页构造器
        Page<Category> page1 = new Page<>(page, pageSize);

        //添加排序条件
        LambdaQueryWrapper<Category> wrapper = new LambdaQueryWrapper<Category>()
                .orderByAsc(Category::getSort);



        categoryService.page(page1,wrapper);


        return R.success(page1);


    }

    /**
     * 根据id删除商品
     * @param ids
     * @return
     */


    @DeleteMapping
    public R<String> delete(Long ids){
        log.info("删除的id是：{}",ids);

        categoryService.remove(ids);

        return R.success("成功删除");
    }

    /**
     * 更新分类
     * @param category
     * @return
     */

    @PutMapping
    public R<String> PutCategory(@RequestBody Category category){

        log.info("更新的数据：{}",category.toString());

        categoryService.updateById(category);

        return R.success("更新成功");

    }

    /**
     * 菜品管理 添加菜品时 查询 菜品分类
     * @param category
     * @return
     */

    @GetMapping("/list")
    public R<List<Category>> getList(Category category){

        log.info("查询菜品分类----》{}",category.toString());

        // 构建条件构造器
        LambdaQueryWrapper<Category> eq = new LambdaQueryWrapper<Category>()
                .eq(category.getType() != null , Category::getType, category.getType())
                .orderByAsc(Category::getSort)
                .orderByAsc(Category::getUpdateTime);


        List<Category> list = categoryService.list(eq);

        return R.success(list);


    }


}
